#' 根据res创建table2
#'
#' @param res U5_mr分析出的res
#' @param tidy_number 清洗数字格式列的功能，可以用Oneclick:::tidy_num查看默认功能,也可以自定义
#'
#' @return table2
#' @export
#'
#' @examples
#'
#' \dontrun{
#'
#' table2<-U6_create_table2(res)
#'
#' # 只作阳性的结果的表
#' ivw_sig<-subset(res,res$`pval_Inverse variance weighted`< 0.05)
#' table2 <-U6_create_table2(ivw_sig)
#'
#'
#' # 如果结局是二分类变量，应该报告OR，如果结局是连续变量，应该报告Beta,
#' # 下面是报告OR的示例，ivw_sig里面只有MR Egger，Weighted median，Inverse variance weighted，MR_PRESSO四种方法
#' library(dplyr)
#' library(flextable)
#' table2 <- U6_create_table2(ivw_sig) %>%
#' select(-c(starts_with('Beta (95% CI)'),"mF","IVW_Q_pval",
#'           "egger_intercept_pval","id.exposure","id.outcome",
#'           ends_with("_Wald ratio"))) %>%  # 去掉Beta (95% CI)开头的列，"_Wald ratio"结尾的列，和其他
#'   rename("Exposure"="exposure","Outcome"="outcome") %>%   # 列名重命名
#'   flextable() %>%            # 变成三线表
#'   bold( part = "header")%>%       # 表头加粗
#'   labelizor( labels = c("OR (95% CI)_MR Egger" = "OR (95% CI)",
#'                         "OR (95% CI)_Weighted median" = "OR (95% CI)",
#'                         "OR (95% CI)_Inverse variance weighted" = "OR (95% CI)",
#'                         "OR (95% CI)_MR_PRESSO" = "OR (95% CI)",
#'                         "pval_MR Egger" = "pval",
#'                         "pval_Weighted median" = "pval",
#'                         "pval_Inverse variance weighted" = "pval",
#'                         "pval_MR_PRESSO" = "pval"), part = "all") %>%   # 列名重命名
#'   add_header_row( values = c("Exposure","Outcome","nSNP","MR Egger",
#'    "Weighted median","Inverse variance weighted","MR_PRESSO"),
#'                   colwidths = c(1,1,1, 2,2,2,2)) %>%   # 表头加一行
#'   merge_v( j = c("Exposure","Outcome","nSNP"),part = "header" )%>%  # 合并表头的这些列
#'   set_table_properties( layout = "autofit") # 表格自动调整
#' table2
#'
#'
#'
#'
#' # dplyr处理数据容易出现的错误
#' # https://www.tidyverse.org/blog/2020/04/dplyr-1-0-0-and-vctrs/
#'
#' }
#'
#'
U6_create_table2 <- function( res, tidy_number =  tidy_num   ){

  require(dplyr)

  df<- res %>% select(c("id.exposure", "id.outcome",
                        "exposure", "outcome",      "nSNP",
                        starts_with( c("OR (95% CI)_","Beta (95% CI)_","pval_")),
                        "mF","IVW_Q_pval","egger_intercept_pval" ))  %>%
    rowwise() %>%
    mutate(across(where(is.numeric), tidy_number))%>%
    ungroup( )

  df$nSNP <- round( as.numeric(df$nSNP)   )

  method <- df %>%
    select(starts_with("OR (95% CI)_")) %>%
    colnames()

  method <- gsub("OR (95% CI)","", method ,
                 fixed = TRUE )


  df1<- df %>% select(c("id.exposure", "id.outcome",
                        "exposure", "outcome",      "nSNP",
                        ends_with( method),"mF","IVW_Q_pval","egger_intercept_pval" )
  )

  return(df1)

}

tidy_num =  function(num){

  if( !is.na(num) ){

  if( abs(num ) < 0.001 ){
    num<-format(num, digits=3,scientific = TRUE)
  }else if( abs( num) > 1000 ){
    num<-format(  num , digits=1,scientific = TRUE)
  }else if( abs( num) < 1 ){
    num<- sprintf("%.3f",round(num,3) )
  }else{
    num<- format(  num,digits=2 )
  }

  }

  return( as.character(num)  )
}

